package com.kcsj.controller.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kcsj.domain.user.Customer;
import com.kcsj.domain.user.Employee;
import com.kcsj.domain.user.User;
import com.kcsj.service.user.CustomerService;
import com.kcsj.service.user.EmployeeService;
import com.kcsj.service.user.UserService;
import com.kcsj.util.JSONUtil;
import com.kcsj.util.MessageHelper;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
/**
 * @author 胡庆阳
 * 29th Nov. 2020
 */
@Slf4j
@WebServlet("/personInfo")
public class PersonInfoController extends HttpServlet {
    //修改个人信息
    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String json = JSONUtil.getJSON(request);
        try {
            JSONObject jsonObject = JSONObject.parseObject(json);
            User user = JSON.parseObject(json,User.class);
            //获取客户的类型
            String type = user.getType();
            if(type.equals("客户")){
                Customer customer = jsonObject.getObject("actor", Customer.class);
                user.setActor(customer);
                CustomerService.getInstance().updateInfo(user);
            }else {
                Employee employee = jsonObject.getObject("actor", Employee.class);
                user.setActor(employee);
                EmployeeService.getInstance().updateInfo(user);
            }
            MessageHelper.addMessage("修改成功");
            String user_json = JSON.toJSONString(user);
            JSONObject message = MessageHelper.addData(user_json);
            response.getWriter().println(message);
        } catch (SQLException e) {
            log.error(e.getMessage());
            JSONObject message = MessageHelper.dateBaseError();
            response.getWriter().println(message);
        } catch (Exception e) {
            JSONObject message = MessageHelper.netWordError();
            e.printStackTrace();
            //响应message到前端
            response.getWriter().println(message);
        }
    }
    //获取个人信息
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取前端请求的信息
        Integer userId = Integer.parseInt(request.getAttribute("userId").toString());
        //创建JSON对象message，以便往前端响应信息
        try {
            User user = UserService.getInstance().find(userId);
            String type = user.getType();
            if(type.equals("员工")){
                User employee = EmployeeService.getInstance().find(userId);
                String employee_json = JSON.toJSONString(employee);
                JSONObject data = MessageHelper.addData(employee_json);
                response.getWriter().println(data);
            }else {
                User customer = CustomerService.getInstance().find(userId);
                String customer_json = JSON.toJSONString(customer);
                JSONObject data = MessageHelper.addData(customer_json);
                response.getWriter().println(data);
            }
        }catch (SQLException e){
            JSONObject message = MessageHelper.dateBaseError();
            e.printStackTrace();
            //响应message到前端
            response.getWriter().println(message);
        }catch(Exception e){
            JSONObject message = MessageHelper.netWordError();
            e.printStackTrace();
            //响应message到前端
            response.getWriter().println(message);
        }
    }

}
